// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   TextElement.java

package ui;

// Referenced classes of package ui:

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextBox;

//            Element, BrowserCanvas, StringUtil

public class TextElement extends Element
    implements CommandListener
{

    public TextElement(int left, int top, String name, int maxlength, int constraint, BrowserCanvas canvas, String value)
    {
        super(left, top);
        text = new TextBox(name, value, maxlength, constraint);
        this.canvas = canvas;
        super.width = canvas.getWidth() - left * 2 - 10;
        text.addCommand(cmdOK);
        text.addCommand(cmdCancel);
        text.setCommandListener(this);
        super.eventable = true;
        super.selectable = true;
        string = value;
        stringfit = StringUtil.getFitString(string, BrowserCanvas.FONTSMALL, super.width - 4);
        this.name = name;
        this.constraint = constraint;
        super.height = BrowserCanvas.FONTSMALL.getHeight() * 2 + 2;
    }

    public void commandAction(Command cmd, Displayable arg1)
    {
        if(cmd == cmdOK)
        {
            string = text.getString();
            if((constraint & 0x10000) != 0x10000)
            {
                stringfit = StringUtil.getFitString(string, BrowserCanvas.FONTSMALL, super.width - 4);
            } else
            {
                int w = BrowserCanvas.FONTSMALL.charWidth('*');
                int len = (super.width - 4) / w;
                if(len > string.length())
                    len = string.length();
                StringBuffer buffer = new StringBuffer();
                for(int i = 0; i < len; i++)
                    buffer.append('*');

                stringfit = buffer.toString();
            }
        }
        canvas.display.setCurrent(canvas);
    }

    public String getString()
    {
        return string;
    }

    public String getName()
    {
        return name;
    }

    public void draw(Graphics g, int x, int y, int startY, int stopY, boolean selected)
    {
        g.setFont(BrowserCanvas.FONTSMALL);
        g.setColor(0);
        y += super.top;
        x += super.left;
        //g.drawString(name, x, y, 20);

        if(selected)
            g.setColor(255);
        //y += BrowserCanvas.FONTSMALL.getHeight();
        g.drawRect(x, y, super.width, BrowserCanvas.FONTSMALL.getHeight() + 2);
        g.setColor(0xffffff);
        g.fillRect(x + 1, y + 1, super.width - 2, BrowserCanvas.FONTSMALL.getHeight());
        g.setColor(0);
        g.drawString(stringfit, x + 2, y + 2, 20);
    }

    public boolean keyPress(int keycode)
    {
        if(keycode == BrowserCanvas.KEYCENTER)
        {
            text.setString(string);
            canvas.display.setCurrent(text);
            return true;
        } else
        {
            return false;
        }
    }

    private static final Command cmdOK = new Command("OK", 4, 1);
    private static final Command cmdCancel = new Command("Batal", 3, 1);
    private TextBox text;
    private BrowserCanvas canvas;
    private String string;
    private String name;
    private String stringfit;
    private int constraint;

}
